package com.jdbc;

import java.sql.*;

public class JDBCDemo {

    public static final String URL = "jdbc:mysql://localhost:3306/mydb?serverTimezone=GMT&useUnicode=true&characterEncoding=UTF-8";
    public static final String USER = "root";
    public static final String PASSWORD = "123456";
    public static void testJDBC(){
        //通过驱动管理器获得连接
        try (Connection connection = DriverManager.getConnection(URL,USER,PASSWORD);){
            System.out.println(connection);
            //通过连接对象获得命令对象
            Statement statement = connection.createStatement();
            System.out.println(statement);
            //实现数据插入
            String sql = "insert into student(name,age,gender,cellphone,address) values('周七',23,'女','13456789999','西安')";
            int rows = statement.executeUpdate(sql);
            if(rows > 0){
                System.out.println("数据插入成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void testQuery(){
        //通过驱动管理器获得连接
        try (Connection connection = DriverManager.getConnection(URL,USER,PASSWORD);){
            //通过连接对象获得命令对象
            Statement statement = connection.createStatement();
            //实现数据查询
            String sql = "select * from student";
            ResultSet resultSet = statement.executeQuery(sql);
            //遍历结果集
            while(resultSet.next()){
                //读取每一列数据
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                String cellphone = resultSet.getString("cellphone");
                String address = resultSet.getString("address");
                System.out.println("id:"+id+" name:"+name+" age:"+age+" gender:"+gender+" cellphone:"+cellphone+" address:"+address);
            }
            resultSet.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void testPreparedStatement(){
        //通过驱动管理器获得连接
        try (Connection connection = DataSourceUtil.getConnection()){
            System.out.println(connection);
            //通过连接对象获得命令对象
            String sql = "insert into student(name,age,gender,cellphone,address) values(?,?,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            //设置参数
            preparedStatement.setString(1,"赵小六");
            preparedStatement.setInt(2,22);
            preparedStatement.setString(3,"男");
            preparedStatement.setString(4,"13456766998");
            preparedStatement.setString(5,"西安");
            //执行命令
            int rows = preparedStatement.executeUpdate();
            if(rows > 0){
                System.out.println("数据插入成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
//        testJDBC();
//        testQuery();
        testPreparedStatement();
    }
}
